package com.xingxue.logistics.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xingxue.logistics.common.vo.Page;
import com.xingxue.logistics.common.vo.RestResult;
import com.xingxue.logistics.common.vo.TreeNode;
import com.xingxue.logistics.dao.domain.SysMenu;
import com.xingxue.logistics.dao.domain.SysMenuExample;
import com.xingxue.logistics.dao.domain.SysRole;
import com.xingxue.logistics.dao.domain.SysRoleExample;
import com.xingxue.logistics.dao.domain.SysRoleMenu;
import com.xingxue.logistics.dao.domain.SysRoleMenuExample;
import com.xingxue.logistics.dao.mapper.SysMenuMapper;
import com.xingxue.logistics.dao.mapper.SysRoleMapper;
import com.xingxue.logistics.dao.mapper.SysRoleMenuMapper;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

import javafx.scene.control.Menu;

/**
 * @author Kris at 2018/10/9 10:07
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    SysMenuMapper menuMapper;
    @Autowired
    SysRoleMapper roleMapper;

    @Autowired
    SysRoleMenuMapper roleMenuMapper;


    @Override
    public List<SysMenu> loadAll() {
        SysMenuExample example = new SysMenuExample();
        example.setOrderByClause(" id asc");
        return menuMapper.selectByExample(example);
    }

    @Override
    public List<TreeNode> createTree(List<SysMenu> menuList) {
        List<TreeNode> treeNodeList = new LinkedList<>();
        for (SysMenu menu : menuList) {
            TreeNode treeNode = new TreeNode(menu.getId(), menu.getParentId(), menu.getName(), menu.getUrl(), null,null);
            treeNodeList.add(treeNode);
        }

        List<TreeNode> result = TreeNode.buildTree(treeNodeList);
        return result;
    }

    @Override
    public PageInfo<SysMenu> searchPage(Integer id, Integer parentId, String menuName,Integer pageNo,Integer pageSize) {
        SysMenuExample example = new SysMenuExample();
        example.setOrderByClause(" id asc");
        SysMenuExample.Criteria criteria = example.createCriteria();
        if(id!=null){
            criteria.andIdEqualTo(id);
        }
        if(parentId != null){
            criteria.andParentIdEqualTo(parentId);
        }
        if(StringUtils.isNotBlank(menuName)){
            criteria.andNameLike("%"+menuName+"%");
        }

        //分页控件，调用PageHelper.startPage(pageNo, pageSize);表示分页开始
        PageHelper.startPage(pageNo, pageSize);
        List<SysMenu> sysMenus = menuMapper.selectByExample(example);
        // 通过这句代码，实例化一个页码对象出来。PageInfo<SysMenu> pageInfo = new PageInfo<>(sysMenus);
        PageInfo<SysMenu> pageInfo = new PageInfo<>(sysMenus);
        return pageInfo;
    }


    @Override
    public RestResult add(SysMenu menu) {
        int i = menuMapper.insertSelective(menu);
        if(i>0){
            return new RestResult(true,"新增成功",null);
        }
        return new RestResult(false,"新增失败",null);
    }

    @Override
    public RestResult update(SysMenu menu) {
        int i = menuMapper.updateByPrimaryKey(menu);
        if(i>0){
            return new RestResult(true,"修改成功",null);
        }
        return new RestResult(false,"修改失败",null);
    }

    @Override
    public RestResult delete(Integer id) {
        int i = menuMapper.deleteByPrimaryKey(id);
        if(i>0){
            return new RestResult(true,"删除成功",null);
        }
        return new RestResult(false,"删除失败",null);
    }

    @Override
    public List<SysMenu> loadMenuByRoleId(Integer roleId) {
        return menuMapper.loadMenuByRoleId(roleId);
    }

    @Override
    public void updateMenuByRoleId(Integer roleId, Integer[] idsArr) {
        SysRoleMenuExample sysRoleMenuExample=new SysRoleMenuExample();
        roleMenuMapper.deleteByExample(sysRoleMenuExample);
        for(int i=0;i<idsArr.length;i++){
            SysRoleMenu sysRoleMenu=new SysRoleMenu();
            sysRoleMenu.setRoleId(roleId);
            sysRoleMenu.setMenuId(idsArr[i]);
            roleMenuMapper.insert(sysRoleMenu);
        }
    }
}
